package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.AddressBookAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.PageResponse;
import com.swz.dcrm.model.PersonalInfo;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.StaffViewModel;
import com.swz.dcrm.util.Tool;
import com.swz.dcrm.wrapper.EmptyWrapper;
import com.xh.baselibrary.callback.OnClickListener;


import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class StaffManagementFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.rv)
    RecyclerView rv;
    int page=1;
    int size=15;
    int total;
    private EmptyWrapper emptyWrapper;
    AddressBookAdapter addressBookAdapter;

    @Inject
    StaffViewModel staffViewModel;
    public static StaffManagementFragment newInstance() {

        return new StaffManagementFragment();
        // Required empty public constructor
    }
    @Override
    public void onLoadRetry() {
        staffViewModel.getStaffList(page,size)
                .observe(this, observer);
    }
    Observer<PageResponse<PersonalInfo>> observer=new Observer<PageResponse<PersonalInfo>>() {
        @Override
        public void onChanged(PageResponse<PersonalInfo> pageBaseResponse) {
            if(pageBaseResponse.isSuccess()){
                if(addressBookAdapter==null){
                    addressBookAdapter=new AddressBookAdapter(getContext(),pageBaseResponse.getData(),1);
                    addressBookAdapter.setOnClickListener(onClickListener);
                    emptyWrapper=getEmptyWrapper(addressBookAdapter,R.mipmap.empty_worker);
                    rv.setAdapter(emptyWrapper);
                }else {
                    addressBookAdapter.refresh(pageBaseResponse.getPage(),pageBaseResponse.getData());
                    emptyWrapper.notifyDataSetChanged();
                }
            }
        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
    @Override
    public int layoutId() {
        return R.layout.fragment_staff_management;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.staff_management_title));
        tvRight.setText(getString(R.string.staff_management_add));
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        smartRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
            if(addressBookAdapter!=null&&addressBookAdapter.getItemCount()>=total){
                smartRefreshLayout.finishLoadmore();
                smartRefreshLayout.setEnableLoadmore(false);
                return;
            }else {
                page++;
                onLoadRetry();
            }
        });
        onLoadRetry();
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.tv_right)
    public void right(){
        go(null,R.id.action_staffManagementFragment_to_staffInformationFragment,null);
    }

    OnClickListener<PersonalInfo>onClickListener=personalInfo -> {
       StaffDetailFragmentArgs.Builder builder=new StaffDetailFragmentArgs.Builder();
       builder.setId(String.valueOf(personalInfo.getId()));
      go(null,R.id.action_staffManagementFragment_to_staffDetailFragment,builder.build().toBundle());
    };
}
